//
//  AreaViewController.m
//  penzer
//
//  Created by caiyc on 2018/9/18.
//  Copyright © 2018年 caiyc. All rights reserved.
//   32323232

#import "AreaViewController.h"
#import "Distic_Cell.h"
#import "City.h"
#import "HandleCityData.h"
#import "AreaHotCell.h"
#import "RechargeViewController.h"
@interface AreaViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)NSMutableArray *dataSouce;
@property(nonatomic, strong) NSMutableArray * ChineseCities;//存放所有未排序的城市信息
@property (nonatomic, strong) UISearchBar *searchBar;//搜索框
@property (nonatomic, strong) NSMutableArray *cities;//存放未处理所有城市
@property(nonatomic, strong) NSMutableArray * fixArray;//存放纯城市信息
@property(nonatomic, strong) NSMutableArray * tempArray;//中间数组
@property(nonatomic, strong) NSArray * letters;//存放开头字母
@property(nonatomic, strong) UITableView * table;// 展示列表
@property(nonatomic, strong) NSString * loctionCity;//定位城市
@property(nonatomic, assign) BOOL isSearch;//是否是search状态
@property(nonatomic,strong)MBProgressHUD *HUD;
@property(nonnull,strong)NSDictionary *cityDic;
@property(nonatomic,strong)NSMutableArray *hot_Arr;
@end


@implementation AreaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self setNavigation:1 andTitle:@"选择区域" andNaviColor:nil andTitleColor:nil];
    self.dataSouce = [NSMutableArray array];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    self.lab2.hidden = 1;
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:_HUD];
    _HUD.label.text = @"数据获取中";
    //细节文字
    _HUD.detailsLabel.text = @"请耐心等待";
    //显示对话框
    [_HUD showAnimated:YES];
    self.hot_Arr = [NSMutableArray array];
  //  [self performSelectorInBackground:@selector(configData) withObject:nil];
    [self loadCit];
    [self loadHotcity];
}
-(void)loadHotcity{
    NSDictionary *param = @{@"ta":@"district_list",@"ac":@"live",@"ctrl":@"common",@"is_hot":@"1"};
    [self weqPostHandle:param success:^(id response) {
        if(response){
            [self.hot_Arr addObjectsFromArray:response[@"data"]];

           [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationTop];
        }
    } failure:^(NSError *error) {
        
    }];
}
-(void)configData{
    self.letters = [NSArray array];
    self.fixArray = [[NSMutableArray alloc] init];
    self.tempArray = [[NSMutableArray alloc] init];//search出来的数据存放
}
-(void)hideHud{
    [_HUD removeFromSuperview];
}

-(void)loadCit{
    self.letters = [NSArray array];
    self.fixArray = [[NSMutableArray alloc] init];
    self.tempArray = [[NSMutableArray alloc] init];//search出来的数据存放
     [_HUD removeFromSuperview];
    NSDictionary *param = @{@"ta":@"district_list",@"ac":@"live",@"ctrl":@"common"};
    [self weqPostHandle:param success:^(id response) {
        if(response){
            NSDictionary *dic = response[@"data"];
            self.cityDic = response[@"data"];
            NSArray *arr = [dic allKeys];
            self.letters = [arr sortedArrayUsingSelector:@selector(compare:)];
            
         
            [self.tableView reloadData];
        }
    } failure:^(NSError *error) {
        
    }];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.letters.count+1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section==0){
        return 1;
    }
    NSString *key = self.letters[section-1];
    NSArray * letterArray = [self.cityDic objectForKey:key];//对应字母所含城市数组
    return letterArray.count;
  //  return self.dataSouce.count;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
  if(section!=0)
    return [self.letters objectAtIndex:section-1];
  else return @"";
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.letters;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{

    return index+1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if(indexPath.section==0){
        AreaHotCell *cell = [[[NSBundle mainBundle]loadNibNamed:@"AreaHotCell" owner:self options:nil]lastObject];
        [cell bindData:self.hot_Arr];
        cell.slecty=^(NSInteger index){
            NSString *ids = self.hot_Arr[index][@"id"];
            [WToast showWithText:@"等级不够，请先充值"];
            RechargeViewController *vc = [[RechargeViewController alloc]init];
            [self pushSecondVC:vc];
            //self.city(self.hot_Arr[index]);
           // self.slecty(ids);
           
        };
        return cell;
    }else{
    static NSString *tipCellIdentifier = @"tipCellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tipCellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                      reuseIdentifier:tipCellIdentifier];
        UILabel *sub = [[UILabel alloc]initWithFrame:CGRectMake(screen_width-145, 0, 120, 60)];
        sub.text = @"限30级";
        sub.textAlignment = NSTextAlignmentRight;
        sub.textColor = [UIColor grayColor];
        sub.font  =[UIFont systemFontOfSize:13];
        [cell.contentView addSubview:sub];
    }
        
    NSString *key = self.letters[indexPath.section-1];
    NSArray * letterArray = [self.cityDic objectForKey:key];//对应字母所含城市数组
    cell.textLabel.text = letterArray[indexPath.row][@"name"];
    cell.textLabel.font = [UIFont systemFontOfSize:13];
       
    return cell;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if(indexPath.section!=0){
        [WToast showWithText:@"等级不够，请先充值"];
        RechargeViewController *vc = [[RechargeViewController alloc]init];
        [self pushSecondVC:vc];

    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section==0){
        NSLog(@"....高度%lu",115+50*(self.hot_Arr.count/3));
        return 115+50*(self.hot_Arr.count/3+1);
    }
    return 60;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)Areabtn:(UIButton *)sender {
    self.lab2.hidden = 1;
    self.lab1.hidden = 0;
    [self.cicle_Btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.area_Btn setTitleColor:BUTTON_COLOR forState:UIControlStateNormal];
}
- (IBAction)cicleBtn:(UIButton *)sender {
    self.lab2.hidden = 0;
    self.lab1.hidden = 1;
    [self.area_Btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.cicle_Btn setTitleColor:BUTTON_COLOR forState:UIControlStateNormal];
}
@end
